using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Adex
{
	/// <summary>
	/// Strongly-typed collection for the MediaType class.
	/// </summary>
	[Serializable]
	public partial class MediaTypeCollection : ActiveList<MediaType, MediaTypeCollection> 
	{	   
		public MediaTypeCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the MediaType table.
	/// </summary>
	[Serializable]
	public partial class MediaType : ActiveRecord<MediaType>
	{
		#region .ctors and Default Settings
		
		public MediaType()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public MediaType(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public MediaType(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public MediaType(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("MediaType", TableType.Table, DataService.GetInstance("Adex"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarMediaTypeX = new TableSchema.TableColumn(schema);
				colvarMediaTypeX.ColumnName = "MediaTypeCode";
				colvarMediaTypeX.DataType = DbType.Int32;
				colvarMediaTypeX.MaxLength = 0;
				colvarMediaTypeX.AutoIncrement = false;
				colvarMediaTypeX.IsNullable = false;
				colvarMediaTypeX.IsPrimaryKey = true;
				colvarMediaTypeX.IsForeignKey = false;
				colvarMediaTypeX.IsReadOnly = false;
				colvarMediaTypeX.DefaultSetting = @"";
				colvarMediaTypeX.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMediaTypeX);
				
				TableSchema.TableColumn colvarMediaTypeName = new TableSchema.TableColumn(schema);
				colvarMediaTypeName.ColumnName = "MediaTypeName";
				colvarMediaTypeName.DataType = DbType.String;
				colvarMediaTypeName.MaxLength = 50;
				colvarMediaTypeName.AutoIncrement = false;
				colvarMediaTypeName.IsNullable = true;
				colvarMediaTypeName.IsPrimaryKey = false;
				colvarMediaTypeName.IsForeignKey = false;
				colvarMediaTypeName.IsReadOnly = false;
				colvarMediaTypeName.DefaultSetting = @"";
				colvarMediaTypeName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMediaTypeName);
				
				TableSchema.TableColumn colvarIdentifier = new TableSchema.TableColumn(schema);
				colvarIdentifier.ColumnName = "Identifier";
				colvarIdentifier.DataType = DbType.String;
				colvarIdentifier.MaxLength = 2;
				colvarIdentifier.AutoIncrement = false;
				colvarIdentifier.IsNullable = true;
				colvarIdentifier.IsPrimaryKey = false;
				colvarIdentifier.IsForeignKey = false;
				colvarIdentifier.IsReadOnly = false;
				colvarIdentifier.DefaultSetting = @"";
				colvarIdentifier.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIdentifier);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["Adex"].AddSchema("MediaType",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("MediaTypeX")]
		public int MediaTypeX 
		{
			get { return GetColumnValue<int>("MediaTypeCode"); }

			set { SetColumnValue("MediaTypeCode", value); }

		}

		  
		[XmlAttribute("MediaTypeName")]
		public string MediaTypeName 
		{
			get { return GetColumnValue<string>("MediaTypeName"); }

			set { SetColumnValue("MediaTypeName", value); }

		}

		  
		[XmlAttribute("Identifier")]
		public string Identifier 
		{
			get { return GetColumnValue<string>("Identifier"); }

			set { SetColumnValue("Identifier", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Adex.CountryMediumCollection CountryMedia()
		{
			return new Adex.CountryMediumCollection().Where(CountryMedium.Columns.MediaType, MediaTypeX).Load();
		}

		public Adex.MediumCollection MediumRecords()
		{
			return new Adex.MediumCollection().Where(Medium.Columns.MediaType, MediaTypeX).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		#region Many To Many Helpers
		
		 
		public Adex.CountryCollection GetCountryCollection() { return MediaType.GetCountryCollection(this.MediaTypeX); }

		public static Adex.CountryCollection GetCountryCollection(int varMediaTypeX)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM Country INNER JOIN CountryMedia ON "+
				"Country.CountryID=CountryMedia.CountryID WHERE CountryMedia.MediaTypeCode=@MediaTypeCode", MediaType.Schema.Provider.Name);
			
			cmd.AddParameter("@MediaTypeCode", varMediaTypeX, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			CountryCollection coll = new CountryCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveCountryMap(int varMediaTypeX, CountryCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE MediaTypeCode=@MediaTypeCode", MediaType.Schema.Provider.Name);
			cmdDel.AddParameter("@MediaTypeCode", varMediaTypeX);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Country item in items)
			{
				CountryMedium varCountryMedium = new CountryMedium();
				varCountryMedium.SetColumnValue("MediaTypeCode", varMediaTypeX);
				varCountryMedium.SetColumnValue("CountryID", item.GetPrimaryKeyValue());
				varCountryMedium.Save();
			}

		}

		public static void SaveCountryMap(int varMediaTypeX, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE MediaTypeCode=@MediaTypeCode", MediaType.Schema.Provider.Name);
			cmdDel.AddParameter("@MediaTypeCode", varMediaTypeX);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					CountryMedium varCountryMedium = new CountryMedium();
					varCountryMedium.SetColumnValue("MediaTypeCode", varMediaTypeX);
					varCountryMedium.SetColumnValue("CountryID", l.Value);
					varCountryMedium.Save();
				}

			}

		}

		public static void SaveCountryMap(int varMediaTypeX , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE MediaTypeCode=@MediaTypeCode", MediaType.Schema.Provider.Name);
			cmdDel.AddParameter("@MediaTypeCode", varMediaTypeX);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				CountryMedium varCountryMedium = new CountryMedium();
				varCountryMedium.SetColumnValue("MediaTypeCode", varMediaTypeX);
				varCountryMedium.SetColumnValue("CountryID", item);
				varCountryMedium.Save();
			}

		}

		
		public static void DeleteCountryMap(int varMediaTypeX) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM CountryMedia WHERE MediaTypeCode=@MediaTypeCode", MediaType.Schema.Provider.Name);
			cmdDel.AddParameter("@MediaTypeCode", varMediaTypeX);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varMediaTypeX,string varMediaTypeName,string varIdentifier)
		{
			MediaType item = new MediaType();
			
			item.MediaTypeX = varMediaTypeX;
			
			item.MediaTypeName = varMediaTypeName;
			
			item.Identifier = varIdentifier;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varMediaTypeX,string varMediaTypeName,string varIdentifier)
		{
			MediaType item = new MediaType();
			
				item.MediaTypeX = varMediaTypeX;
				
				item.MediaTypeName = varMediaTypeName;
				
				item.Identifier = varIdentifier;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string MediaTypeX = @"MediaTypeCode";
			 public static string MediaTypeName = @"MediaTypeName";
			 public static string Identifier = @"Identifier";
						
		}

		#endregion
	}

}

